//
//  SWWebBrowserViewController.swift
//  MiU
//
//  Created by linhan on 16/1/26.
//  Copyright © 2016年 MiU. All rights reserved.
//

import Foundation
import UIKit
class SWWebBrowserViewController:SWBaseViewController,UIWebViewDelegate
{
    var url:String = ""
    
    private var _webView:UIWebView?
    private var _indicatorView:UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    
    init(url:String)
    {
        self.url = url
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if appearedFirstTime
        {
            appearedFirstTime = false
            if let url = URL(string: url)
            {
                _webView?.loadRequest(URLRequest(url: url))
            }
        }
    }
    
    private func setup()
    {
        _webView = UIWebView(frame: view.bounds)
        _webView?.delegate = self
        view.addSubview(_webView!)
        
        _indicatorView.center = view.center
        view.addSubview(_indicatorView)
    }
    
    func webViewDidStartLoad(_ webView: UIWebView)
    {
        _indicatorView.startAnimating()
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        _indicatorView.stopAnimating()
    }
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
    {
        _indicatorView.stopAnimating()
    }
    
    
    
}
